<?php
/**
 * IG_Filter
 *
 * @author mmxie
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Filter.php 929 2011-10-27 07:52:50Z mmxie $
 */
class IG_Filter
{
    protected $_default = null;

    protected $_calls   = array();

    protected $_must    = false;

    protected $_value;

    protected $_paramKey;

    public function __construct($config)
    {
        if (isset($config['default'])) {
            $this->_default = $config;
        }

        if (isset($config['call'])) {
            $this->_calls = (array) $config['call'];
        }

        if (isset($config['must'])) {
            $this->_must = (bool) $config['must'];
        }
    }

    public function setValue($value)
    {
        $this->_value = $value;
        return $this;
    }

    public function getValue()
    {
        return $this->_value;
    }

    public function setParamKey($value)
    {
        $this->_paramKey = $value;
        return $this;
    }

    public function getDefault()
    {
        return $this->_default;
    }

    public function checkEmpty()
    {
        if ($this->_must && is_null($this->_value)) {
            $this->_error("请求参数'$this->_paramKey'不能为空");
        }
    }

    public function filterFunctions()
    {
        foreach ($this->_calls as $call) {
            if (!call_user_func($call, $this->_value)) {
                $this->_error('验证不通过');
            }
        }
    }

    public function useFunctions()
    {
        foreach ($this->_ as $variable) {
            ;
        }
    }

    protected function _error($msg)
    {
        throw new IG_Exception($msg);
    }

}